package DyingBaby.backend.common;

/**
 * 缓存配置管理类
 * 用于控制缓存统计信息和日志的显示
 */
public class CacheConfig {
    
    // 默认配置值
    private static final boolean DEFAULT_ENABLE_STATS = true;
    private static final boolean DEFAULT_ENABLE_LOGGING = true;
    private static final boolean DEFAULT_ENABLE_AUTO_PRINT = false;
    private static final CacheStrategy DEFAULT_CACHE_STRATEGY = CacheStrategy.REFERENCE_COUNT;
    
    // 配置选项
    private static boolean enableStats = DEFAULT_ENABLE_STATS;
    private static boolean enableLogging = DEFAULT_ENABLE_LOGGING;
    private static boolean enableAutoPrint = DEFAULT_ENABLE_AUTO_PRINT;
    private static CacheStrategy cacheStrategy = DEFAULT_CACHE_STRATEGY;
    
    // 静态初始化块，从系统属性读取配置
    static {
        loadFromSystemProperties();
    }
    
    /**
     * 从系统属性加载配置
     */
    private static void loadFromSystemProperties() {
        // 从系统属性读取配置
        String statsProp = System.getProperty("mydb.cache.stats", "true");
        String loggingProp = System.getProperty("mydb.cache.logging", "true");
        String autoPrintProp = System.getProperty("mydb.cache.autoPrint", "false");
        String strategyProp = System.getProperty("mydb.cache.strategy", "REFERENCE_COUNT");
        
        enableStats = Boolean.parseBoolean(statsProp);
        enableLogging = Boolean.parseBoolean(loggingProp);
        enableAutoPrint = Boolean.parseBoolean(autoPrintProp);
        
        // 解析缓存策略
        try {
            cacheStrategy = CacheStrategy.valueOf(strategyProp.toUpperCase());
        } catch (IllegalArgumentException e) {
            cacheStrategy = DEFAULT_CACHE_STRATEGY;
        }
    }
    
    /**
     * 是否启用统计功能
     */
    public static boolean isStatsEnabled() {
        return enableStats;
    }
    
    /**
     * 是否启用日志记录
     */
    public static boolean isLoggingEnabled() {
        return enableLogging;
    }
    
    /**
     * 是否启用自动打印统计信息
     */
    public static boolean isAutoPrintEnabled() {
        return enableAutoPrint;
    }
    
    /**
     * 获取当前缓存策略
     */
    public static CacheStrategy getCacheStrategy() {
        return cacheStrategy;
    }
    
    /**
     * 设置统计功能开关
     */
    public static void setStatsEnabled(boolean enabled) {
        enableStats = enabled;
    }
    
    /**
     * 设置日志记录开关
     */
    public static void setLoggingEnabled(boolean enabled) {
        enableLogging = enabled;
    }
    
    /**
     * 设置自动打印统计信息开关
     */
    public static void setAutoPrintEnabled(boolean enabled) {
        enableAutoPrint = enabled;
    }
    
    /**
     * 设置缓存策略
     */
    public static void setCacheStrategy(CacheStrategy strategy) {
        cacheStrategy = strategy;
    }
    
    /**
     * 重置所有配置为默认值
     */
    public static void resetToDefaults() {
        enableStats = DEFAULT_ENABLE_STATS;
        enableLogging = DEFAULT_ENABLE_LOGGING;
        enableAutoPrint = DEFAULT_ENABLE_AUTO_PRINT;
        cacheStrategy = DEFAULT_CACHE_STRATEGY;
    }
    
    /**
     * 获取当前配置信息
     */
    public static String getConfigInfo() {
        return String.format("缓存配置 - 统计: %s, 日志: %s, 自动打印: %s, 策略: %s", 
                enableStats ? "启用" : "禁用",
                enableLogging ? "启用" : "禁用", 
                enableAutoPrint ? "启用" : "禁用",
                cacheStrategy.getDescription());
    }
    
    /**
     * 批量设置配置
     */
    public static void setConfig(boolean stats, boolean logging, boolean autoPrint) {
        enableStats = stats;
        enableLogging = logging;
        enableAutoPrint = autoPrint;
    }
    
    /**
     * 批量设置配置（包含缓存策略）
     */
    public static void setConfig(boolean stats, boolean logging, boolean autoPrint, CacheStrategy strategy) {
        enableStats = stats;
        enableLogging = logging;
        enableAutoPrint = autoPrint;
        cacheStrategy = strategy;
    }
}
